<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class EntryController extends Controller{
    public $layout='column2';  
    public function actionIndex(){
        $chapter=  empty($_REQUEST['chapter'])?'':$_REQUEST['chapter'];
        $verse= empty($_REQUEST['verse'])?'':$_REQUEST['verse'];
        $data=array();
        $key='';
        $chapterlist=array();
        $chapters= Chapter::model()->findAll('state=1 order by sequence asc');
        $chapterlist[]='请选择,您要查看的章名称';
        foreach ($chapters as $cr){
            $chapterlist[$cr->id]=  Yii::app()->params['chapter'][$cr->sequence].' '.$cr->name;
        }
        if(!empty($chapter) && !empty($verse)){
//            $model =  Entry::model()->findAll('chapter_id=:chapter and verse_id=:verse_id',array(':chapter'=>$chapter,':verse'=>$verse));
            $sql="select * from entry where chapter_id=$chapter and verse_id=$verse";
            $data=  Yii::app()->db->createCommand($sql)->queryAll($sql);
        }
        $this->render('index',array('data'=>$data,'chapterlist'=>$chapterlist,'key'=>$key));
    }
    
    public function actionCreate(){
        $model = new Entry();
        $chapterlist=array();
        $chapter= Chapter::model()->findAll('state=1 order by sequence asc');
        $chapterlist[]='请选择,您要查看的章名称';
        foreach ($chapter as $cr){
            $chapterlist[$cr->id]=  Yii::app()->params['chapter'][$cr->sequence].' '.$cr->name;
        }
        if(isset($_POST['Entry'])){
            $model->attributes=$_POST['Entry'];
            if($model->validate() && $model->save()){
                $this->redirect($this->createUrl('/entry/index',array('chapter'=>$model->chapter_id,'verse'=>$model->verse_id)));
            } 
        }
        $this->render('_form',array('model'=>$model,'chapter'=>$chapterlist));
    }
    
    public function actionVerseList(){
        $chpater=$_POST['cid'];
        $sql="select * from verse where chapter_id=$chpater";
        $data=  Yii::app()->db->createCommand($sql)->queryAll();
        echo CHtml::tag('option', array('value'=>''),CHtml::encode('请选择'),true);
        foreach ($data as $da){
            echo CHtml::tag('option', array('value'=>$da['id']),CHtml::encode('第'.$da['sequence']."节 ".$da['name']),true);
        }
    }
    
    public function actionEntryList(){
        $entry=$_POST['eid'];
        $sql="select * from entry where verse_id=$entry";
        $data=  Yii::app()->db->createCommand($sql)->queryAll();
        foreach ($data as $da){
            echo CHtml::tag('option', array('value'=>$da['id']),CHtml::encode('第'.$da['sequence']."节 ".$da['name']),true);
        }
    }
}